/** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
**/

var networkService = null;

var ConnectionMonitor = {
	connectionType: null,
	networkChangedCallback: null,
	startMonitorForNetwork: function(networkChangedCallback) {
		logDebug("Initializing ConnectionMonitor...");
		ConnectionMonitor.networkChangedCallback = networkChangedCallback;
		if (!networkService && phone) {
			try {
				networkService = device.getServiceObject("Service.SysInfo", "ISysInfo");
			} catch (e) {
				logDebug("Error setting up ConnectionMonitor: " + e.toString());
			}
		}
		
		if (networkService) {
			var connectionStatusCriteria = {
				Entity: "Connectivity",
				Key: "ConnectionStatus"
			}
			try {
				networkService.ISysInfo.GetNotification(connectionStatusCriteria, ConnectionMonitor.connectionStatusChanged);
			} catch (e) {
				logDebug("Error setting up ConnectionMonitor: " + e.toString());
			}
			
			var networkCriteria = {
				Entity: "Network",
				Key: "CurrentNetwork"
			}
			try {
				networkService.ISysInfo.GetNotification(networkCriteria, ConnectionMonitor.networkStatusChanged);
			} catch (e) {
				logDebug("Error setting up ConnectionMonitor: " + e.toString());
			}
			
			ConnectionMonitor.getConnectionInfo();
		}
		logDebug("ConnectionManager initialization complete");
	},
	
	connectionStatusChanged: function(transId, eventCode, result){
		logDebug("Connection status changed in ConnectionMonitor");
		if (!result || !result.ReturnValue || result.ErrorCode != 0) 
			return;
				
		//if wlan connection is made in offline mode, we get just conninfo object and not the list _AAARGH_
		var connInfo;
		if (result.ReturnValue.ConnectionStatus != null) {
			connInfo = result.ReturnValue;
		} else {
			if (result.ReturnValue.ConnectionList) {
				connInfo = result.ReturnValue.ConnectionList.getNext();
			} else {
				connInfo = result.ReturnValue;
			}				
		} 
		
		if (!OnlineMonitor.isOnline()) {
			ConnectionMonitor.connectionType = -2
		} else {
			if (connInfo) {
				if (connInfo.ConnectionStatus == 0) {
					ConnectionMonitor.connectionType = -2;
				} else {
					ConnectionMonitor.connectionType = connInfo.ConnectionType;
				}
			} else {
				ConnectionMonitor.connectionType = -2;
			}				
		}
		
		if (ConnectionMonitor.networkChangedCallback) {
			ConnectionMonitor.networkChangedCallback(ConnectionMonitor.connectionType);
		}
	},
	
	getConnectionInfo: function(){
		try {
			var connectionStatusCriteria = {
				Entity: "Connectivity",
				Key: "ConnectionStatus"
			}
			networkService.ISysInfo.GetInfo(connectionStatusCriteria, ConnectionMonitor.connectionStatusChanged);
		} catch (ex) {
			logDebug("Error getting connection info in ConnectionMonitor: " + e.toString());
		}
	},
	
	oldNetworkMode: -1,
	networkStatusChanged: function(transId, eventCode, result){
		logDebug("Network status changed in ConnectionMonitor");
		if (result.ErrorCode == 0 && result.ReturnValue) {
			if (result.ReturnValue.NetworkMode != ConnectionMonitor.oldNetworkMode) {
				ConnectionMonitor.oldNetworkMode = result.ReturnValue.NetworkMode;
				ConnectionMonitor.getConnectionInfo();
			}
		}
	},
	
	changeNetworkType: function(connectionType) {
		ConnectionMonitor.connectionType = connectionType;
		if (ConnectionMonitor.networkChangedCallback) {
			ConnectionMonitor.networkChangedCallback(ConnectionMonitor.connectionType);
		}
	},
	
	getMyNetworkType: function() {
		
		if (!OnlineMonitor.isOnline()) {
			return "OFFLINE";
		}
		
		if (ConnectionMonitor.connectionType == null) 
			return "UNKNOWN";
		
		switch (ConnectionMonitor.connectionType) {
			case -1:
				return "UNKNOWN";
			case 0:
				return "CSD";
			case 1:
				return "WCDMA";
			case 2:
				return "LAN";
			case 3:
				return "CDMA2000";
			case 4:
				return "GPRS";
			case 5:
				return "HSCSD";
			case 6:
				return "EDGE,EGPRS";
			case 7:
				return "WLAN";
			case 8:
				return "BLUETOOTH";
			case 9:
				return "VIRTUALVPN";
		}
		
		return "UNKNOWN";
	},
	
	is3G: function(connType) {
		return ((connType == "WCDMA") || (connType == "CDMA2000"));		
	},
	
	isWiFi: function(connType) {
		return (connType == "WLAN");
	},
	
	is2G: function(connType) {
		return ((connType == "GPRS") || (connType == "EDGE,EGPRS") || (connType == "CSD") || (connType == "HSCSD"));
	}	
}
